package com.light.component;

import atlantafx.base.controls.Card;
import atlantafx.base.controls.ModalPane;
import atlantafx.base.controls.Spacer;
import atlantafx.base.layout.ModalBox;
import atlantafx.base.theme.Tweaks;
import com.light.model.GitProject;
import com.light.thread.FxAsyncTask;
import com.light.util.H2PoolUtils;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;

public class EditorPane extends ModalBox {

    private final ModalPane modalPane;
    protected final Card content = new Card();
    private final Label header = new Label();
    private final TextArea editor = new TextArea();

    private final Text describeLabel;
    private final Text remarkLabel;
    private final Text updateTimeText;

    private GitProject gitProject;

    private boolean remark;

    public EditorPane(ModalPane modalPane, Text describeLabel, Text remarkLabel, Text updateTimeText) {
        super(modalPane);
        this.modalPane = modalPane;
        this.modalPane.setPersistent(true);
        this.describeLabel = describeLabel;
        this.remarkLabel = remarkLabel;
        this.updateTimeText = updateTimeText;
        createView();
    }

    private void createView() {
        content.setHeader(header);
        content.getStyleClass().add(Tweaks.EDGE_TO_EDGE);
        content.setMaxSize(700, 550);
        editor.setWrapText(true);
        content.setBody(editor);
        content.setFooter(createDefaultFooter());

        // IMPORTANT: this guarantees client will use correct width and height
        setMinWidth(USE_PREF_SIZE);
        setMaxWidth(USE_PREF_SIZE);
        setMinHeight(USE_PREF_SIZE);
        setMaxHeight(USE_PREF_SIZE);

        AnchorPane.setTopAnchor(content, 0d);
        AnchorPane.setRightAnchor(content, 0d);
        AnchorPane.setBottomAnchor(content, 0d);
        AnchorPane.setLeftAnchor(content, 0d);

        addContent(content);
    }

    private HBox createDefaultFooter() {
        var okBtn = new Button("确定");
        okBtn.getStyleClass().add("form-action");
        okBtn.setOnAction(event -> {
            String htmlText = editor.getText();
            if (remark) {
                if (htmlText.equals(gitProject.remark().get())) {
                    close();
                    return;
                }
                gitProject.remark().set(htmlText);
                remarkLabel.setText(htmlText);
            } else {
                if (htmlText.equals(gitProject.description().get())) {
                    close();
                    return;
                }
                gitProject.description().set(htmlText);
                describeLabel.setText(htmlText);
            }

            // 更新入库
            FxAsyncTask.runOnce(System.currentTimeMillis(), "更新项目 " + gitProject.name().get(),
                    () -> H2PoolUtils.updateGitProject(gitProject),
                    () -> updateTimeText.setText(gitProject.updateTime().get()));
            close();
        });

        var closeBtn = new Button("取消");
        closeBtn.getStyleClass().add("form-action");
        closeBtn.setCancelButton(true);
        closeBtn.setOnAction(e -> close());

        var footer = new HBox(10, new Spacer(), okBtn, closeBtn);
        footer.getStyleClass().add("footer");
        footer.setAlignment(Pos.CENTER_RIGHT);
        VBox.setVgrow(footer, Priority.NEVER);

        return footer;
    }

    public void show(GitProject gitProject, boolean remark) {
        if (remark) {
            header.setText("备 注");
            editor.setText(gitProject.remark().get());
        } else {
            header.setText("描 述");
            editor.setText(gitProject.description().get());
        }
        this.remark = remark;
        this.gitProject = gitProject;

        modalPane.show(this);
    }
}
